﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using Com;
using Core;
using Models;
using PetaPoco;

namespace Controllers
{
    public class MyFunc
    {
        private string getAllClass(int id)
        {
            var core=new categoryCore();
            var result = id + ",";
            var list=core.GetList(0, " and fid=" + id, "ordnum desc");
            for (var i = 0; i < list.Count; i++)
            {
                result += list[i].ID + ",";
                var jlist = core.GetList(0, " and fid=" + list[i].ID, "ordnum desc");
                for (var j = 0; j < jlist.Count; j++)
                {
                    result += jlist[j].ID + ",";
                }
            }
            result = result.TrimEnd(',');
            return result;
        }
        //获取内容列表
        public List<cxinxiModel> GetList(int top=50,int classid=0,string flag="",string order= "a.ordnum desc", int img=0,string classids="",bool allclass=true,int sid=1)
        {
            List<object> argsList=new List<object>();
            string where= " and a.addtime<=" + MysqlSqlite.GetDateTime(); 
            if (classid > 0)
            {
                if (allclass)
                {
                    where+=" and classid in ("+getAllClass(classid)+")";
                }
                else
                {
                    var argsvalue = argsList.Count;
                    where += " and classid=@" + argsvalue;
                    argsList.Add(classid);
                }
            }

            if (!string.IsNullOrEmpty(flag))
            {
                where += " and flag=@" + argsList.Count;
                argsList.Add(flag);
            }
            if (img == 1)
            {
                where += " and a.pic <>''";
            }
            if (img == 2)
            {
                where += " and (a.pic='' or a.pic is null)";
            }
            if (!string.IsNullOrEmpty(classids))
            {
                where+=" and classid in ("+ classids + ")";
            }
            return new xinxiCore().GetList(top, where, order, argsList.ToArray());
        }

        //获取分页列表
        public Page<cxinxiModel> GetPageList(int page,int pagesize, int classid = 0, string flag = "", string order = "a.ordnum desc", int img = 0, string classids = "", bool allclass = true, int sid = 1)
        {
            List<object> argsList = new List<object>();
            string where = " and a.addtime<=" + MysqlSqlite.GetDateTime();
            if (classid > 0)
            {
                if (allclass)
                {
                    where += " and classid in (" + getAllClass(classid) + ")";
                }
                else
                {
                    var argsvalue = argsList.Count;
                    where += " and classid=@" + argsvalue;
                    argsList.Add(classid);
                }
            }

            if (!string.IsNullOrEmpty(flag))
            {
                where += " and flag=@" + argsList.Count;
                argsList.Add(flag);
            }
            if (img == 1)
            {
                where += " and pic <>''";
            }
            if (img == 2)
            {
                where += " and (pic='' or pic is null)";
            }
            if (!string.IsNullOrEmpty(classids))
            {
                where += " and classid in (@" + argsList.Count + ")";
                argsList.Add(classids);
            }
            return new xinxiCore().GetPage(page,pagesize, where, order, argsList.ToArray());
        }
        //获取分页列表
        public Page<cxinxiModel> GetPageListKeySearch(int page, int pagesize, string key = "", string flag = "", string order = "a.ordnum desc", int img = 0, string classids = "", int sid = 1)
        {
            List<object> argsList = new List<object>();
            string where = " and a.addtime<=" + MysqlSqlite.GetDateTime();
            if (!string.IsNullOrEmpty(key))
            {
                where += " and a.title like @" + argsList.Count+ "";
                argsList.Add($"%{key}%");
            }

            if (!string.IsNullOrEmpty(flag)) 
            {
                where += " and flag=@" + argsList.Count;
                argsList.Add(flag);
            }
            if (img == 1)
            {
                where += " and pic <>''";
            }
            if (img == 2)
            {
                where += " and (pic='' or pic is null)";
            }
            if (!string.IsNullOrEmpty(classids))
            {
                where += " and classid in (@" + argsList.Count + ")";
                argsList.Add(classids);
            }
            return new xinxiCore().GetPage(page, pagesize, where, order, argsList.ToArray());
        }
        //获取分类列表
        public List<categoryEntity>GetCList(int top = 50, int classid = 0, string order = "ordnum desc", int img = 0, string classids = "", int sid = 1)
        {
            List<object> argsList = new List<object>();
            string where = String.Empty;
            if (classid > 0)
            {
                where += " and fid=@" + argsList.Count;
                argsList.Add(classid);
            }
            if (img == 1)
            {
                where += " and pic <>''";
            }
            if (img == 2)
            {
                where += " and (pic='' or pic is null)";
            }
            if (!string.IsNullOrEmpty(classids))
            {
                where += " and fid in (@" + argsList.Count + ")";
                argsList.Add(classids);
            }
            return new categoryCore().GetList(top, where, order, argsList.ToArray());
        }
        //获取一条详细数据
        public xinxiEntity GetOne(int ID,string lx="",int classid=0,string classids="", string flag = "", int sid = 1)
        {
            var core = new xinxiCore();
            if (lx != "")
            {
                string where = string.Empty;
                string order = "ordnum desc,ID desc";
                List<object> argsList = new List<object>();
                if (!string.IsNullOrEmpty(flag))
                {
                    where += " and flag=@" + argsList.Count;
                    argsList.Add(flag);
                }
                if (classid > 0)
                {
                    where += " and classid=@" + argsList.Count;
                    argsList.Add(classid);
                }

                if (!string.IsNullOrEmpty(classids))
                {
                    where += " and classid in (" + classids + ")";
                }
                switch (lx)
                {
                    case "prev":
                        where += " and ID<@" + argsList.Count;
                        break;
                    case "next":
                        where += " and ID>@" + argsList.Count;
                        order = "ordnum asc,ID asc";
                        break;
                }

                argsList.Add(ID);
                var list= core.GetOneByWhere(where,order,argsList.ToArray());
                return list;
            }
            else
            {
                return core.GetOne(ID);
            }
        }
        //获取类别
        public categoryEntity GetClass(int id)
        {
            return new categoryCore().GetOne(id);
        }
        //获取简单内容
        public jxinxiEntity GetJxinxi(string flag)
        {
            return new jxinxiCore().GetOneByJian(flag);
        }
        public jxinxiEntity GetJxinxiByTitle(string title)
        {
            return new jxinxiCore().GetOneByTitle(title);
        }
        public List<liuyanEntity> GetLiuyan(string code)
        {
            var objs = new List<object>();
            objs.Add(code);
            return new liuyanCore().GetList(0, " and code=@0", " zt desc", objs);
        }
        public List<navEntity> GetNav(bool ismobile=false,int classid = 0, int sid = 1)
        {
            List<object> argsList = new List<object>();
            string where = String.Empty;
                where += " and classid=@" + argsList.Count;
                argsList.Add(classid);
            where += " and ismobile=@" + argsList.Count;
            argsList.Add(ismobile);

            var order = "ordnum asc,ID asc";
            return new navCore().GetList(50,where,order, argsList.ToArray());
        }
        public List<linksEntity> GetLink(bool ismobile = false, int sid = 1,int img=0)
        {
            List<object> argsList = new List<object>();
            string where = " and endtime>="+ MysqlSqlite.GetDateTime();
            where += " and ismobile=@" + argsList.Count;
            argsList.Add(ismobile);
            switch (img)
            {
                case 1:
                    where += " and img <> ''";
                    break;
                case 2:
                    where += " and (img='' or img is null)";
                    break;
            }

            var order = "ordnum desc";
            return new linksCore().GetList(0, where, order, argsList.ToArray());
        }

        public List<admodel> GetAdList(string flag, int sid = 1)
        {
            List<object> argsList = new List<object>();
            string where = " and b.tag=@" + argsList.Count +" and b.show=1 and b.endtime>="+MysqlSqlite.GetDateTime();
            argsList.Add(flag);
            return new adCore().GetList(0, where, "ordnum desc", argsList.ToArray());
        }

        public List<toupiaoEntity> GetToupiaoList(int cid=-1, int sid = 1)
        {
            List<object> argsList = new List<object>();
            string where = string.Empty;
            if (cid > -1)
            {
                where = " and cid=@" + argsList.Count;
                argsList.Add(cid);
            }
            return new toupiaoCore().GetList(where, "ordnum desc", argsList.ToArray());
        }
        public static string Length(string str, int length, string wb = "...")
        {
            var result = str;
            if (str.Length > length)
            {
                result = str.Substring(0, length) + wb;
            }
            return result;
        }
    }
}
